// 封装分类数据相关的业务代码
import { getCategoryAPI } from "@/apis/category";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { onBeforeRouteUpdate } from "vue-router";

export function useCategory() {
  const categoryData = ref({});
  const route = useRoute();
  const getCategoryAPIA = async (id = route.params.id) => {
    const res = await getCategoryAPI(id);
    categoryData.value = res.result;
  };
  onMounted(() => getCategoryAPIA());
  //路由参数发生变化时，重新发送分类数据接口
  onBeforeRouteUpdate((to) => {
    // console.log("路由变化了");
    getCategoryAPIA(to.params.id);
  });
  return {
    categoryData,
  };
}
